// ==UserScript==
// @name         bilibili vip remover
// @name:zh-CN   bilibili大会员削除
// @namespace    myfreeer
// @version      0.11
// @description  remove the 'become-vip' link, repalce red names to original, and replace emoji to pure text
// @description:zh-CN  去除“成为大会员”链接，将红名替换为原始格式，替换表情为纯文字
// @author       myfreeer
// @match        http://*.bilibili.com/*
// @match        http://*.bilibili.com/
// @match        https://*.bilibili.com/*
// @match        https://*.bilibili.com/
// @license      MIT
// @grant        GM_getValue
// @grant        GM_setValue
// ==/UserScript==
//edited from http://javascript.ruanyifeng.com/dom/mutationobserver.html
(function (win) {
  'use strict';

  var listeners = [];
  //var doc = window.document;
  //var MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
  var observer;

  function ready(selector, fn) {
    // 储存选择器和回调函数
    listeners.push({
      selector: selector,
      fn: fn
    });
    if (!observer) {
      // 监听document变化
      observer = new MutationObserver(check);

      observer.observe(document.documentElement, {
        childList: true,
        subtree: true,
        attributes: true,
        attributeFilter: ['style', 'class']
      });
    }
    // 检查该节点是否已经在DOM中
    check();
  }

  function check() {
    // 检查是否匹配已储存的节点
    for (var i = 0; i < listeners.length; i++) {
      var listener = listeners[i];
      // 检查指定节点是否有匹配
      var elements = document.querySelectorAll(listener.selector);
      for (var j = 0; j < elements.length; j++) {
        var element = elements[j];
        // 确保回调函数只会对该元素调用一次
        //if (!element.ready) {
        //    element.ready = true;
        //    // 对该节点调用回调函数
        listener.fn.call(element, element);
        //}
      }
    }
  }

  // 对外暴露ready
  win.ready = ready;
})(window);

//Compiled by Babel from https://gist.github.com/myfreeer/99111e7f10767770dc399e24bf6ab9b3
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }

//from https://lvwenhan.com/web-front/374.html
function removeClass(obj, cls) {
  var obj_class = ' ' + obj.className + ' '; //获取 class 内容, 并在首尾各加一个空格. ex) 'abc        bcd' -> ' abc        bcd '
  obj_class = obj_class.replace(/(\s+)/gi, ' '); //将多余的空字符替换成一个空格. ex) ' abc        bcd ' -> ' abc bcd '
  var removed = obj_class.replace(' ' + cls + ' ', ' '); //在原来的 class 替换掉首尾加了空格的 class. ex) ' abc bcd ' -> 'bcd '
  removed = removed.replace(/(^\s+)|(\s+$)/g, ''); //去掉首尾空格. ex) 'bcd ' -> 'bcd'
  obj.className = removed; //替换原来的 class.
  return obj.className;
}

function addStyle(css) {
  if (!css) return false;
  var head = document.head || document.getElementsByTagName('head')[0],
    style = document.createElement('style');
  style.type = 'text/css';
  if (style.styleSheet) {
    style.styleSheet.cssText = css;
  } else {
    style.appendChild(document.createTextNode(css));
  }
  head.appendChild(style);
}

var obj = JSON.parse(GM_getValue('emoji', '{}'));
var refresh = function refresh() {
  return fetch('https://api.bilibili.com/x/v2/reply/emojis').then(function (e) {
    return e.json();
  }).then(function (json) {
    obj = {};
    if (!json.data) return;
    json.data.forEach(function (a) {
      return a.emojis && a.emojis.forEach(function (e) {
        return e.name && e.url && e.url.match(/^https?/) && (obj[new URL(e.url).pathname] = e.name);
      });
    });
    obj.time = Date.now();
    GM_setValue('emoji', JSON.stringify(obj));
  });
};
if (!(obj.time && Date.now() - 3600 * 1000 * 24 * 7 > obj.time)) refresh();
var img2str = function img2str(e) {
  if (!(e && e.alt && e.src && e.src.match('http'))) return;
  var name = '[' + e.alt + ']';
  var path = new URL(e.src).pathname.replace(/@[0-9]+w_[0-9]+h\.webp$/, '');
  if (path && obj[path]) name = obj[path];
  e.outerHTML = ' ' + name + ' ';
};

var becomevip = document.getElementById('i_menu_become_vip');
if (becomevip) becomevip.remove();
[].concat(_toConsumableArray(document.getElementsByClassName('b-vip-red')), _toConsumableArray(document.getElementsByClassName('vip-red-name'))).map(function (e) {
  return removeClass(e, 'b-vip-red') && removeClass(e, 'vip-red-name');
});
[].concat(_toConsumableArray(document.getElementsByClassName('b-vip-emoji'))).map(function (e) {
  return e.outerHTML = e.alt;
});
[].concat(_toConsumableArray(document.getElementsByClassName('h-vipType'))).map(function (e) {
  return e.remove && e.remove();
});
[].concat(_toConsumableArray(document.querySelectorAll('span.user-auth-subscript'))).map(function (e) {
  return e.remove && e.title.match("大会员") && e.remove();
});
[].concat(_toConsumableArray(document.querySelectorAll('#bbComment > .bb-comment > .comment-list > div > .con > p > img'))).map(img2str);
document.querySelectorAll('a[href="//account.bilibili.com/account/big"]').forEach(function (e) {
  return e.parentNode.className == 'nav-item' && e.parentNode.remove();
});
ready('.b-vip-red', function (e) {
  return removeClass(e, 'b-vip-red');
});
ready('.vip-red-name', function (e) {
  return removeClass(e, 'vip-red-name');
});
ready('.b-vip-emoji', function (e) {
  return e.outerHTML = e.alt;
});
ready('#bbComment > .bb-comment > .comment-list > div > .con > p > img', img2str);
ready('.h-vipType', function (e) {
  return e.remove && e.remove();
});
ready('span.user-auth-subscript', function (e) {
  return e.remove && e.title.match("大会员") && e.remove();
});
ready('.nav-item', function (e) {
  return e.innerHTML.match("account.bilibili.com/account/big") && e.remove();
});
addStyle('.user-auth-subscript{background-image: url() !important}.h-vipType,#i_menu_become_vip{display:none !important}');